package com.woniuxy.cloud.procurementassembly.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.woniuxy.cloud.dto.BaseDto;
import com.woniuxy.cloud.procurementassembly.param.BaseParam;


/**
 * 分页的工具类
 *
 * @author 周川雄
 * @date 2022/1/3 20:38
 */
public class PageMysqlUtil {





    /**
     * mysql分页查询
     *
     * @param dto      具体实体类dto的类型
     * @param iService 操作数据库对应的service
     * @param <T>      泛型
     * @return BaseDto<T>   返回dto，包含当前页，页数，具体实体类dto集合
     */
    public static <T, E> BaseDto<T> pageQuery(Class<T> dto, IService iService, QueryWrapper<E> queryWrapper, BaseParam param) {

//        PageQueryParam param = new PageQueryParam();
//
        //设置默认值
        if (param.getIndexPage() == null) {
            param.setIndexPage(1L);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(3L);
        }


        Page<T> page;
        if (queryWrapper == null) {
            page = (Page<T>) iService.page(new Page<>(param.getIndexPage(), param.getPageSize()));
        } else {
            page = (Page<T>) iService.page(new Page<>(param.getIndexPage(), param.getPageSize()), queryWrapper);
        }

        BaseDto<T> baseDto = new BaseDto<>();
        baseDto.setIndexPage(page.getCurrent());
        baseDto.setPageCount(page.getPages());
        //判断是否有上一页
        baseDto.setHasPrevious(page.hasPrevious());
        //判断是否有下一页
        baseDto.setHasNext(page.hasNext());
        baseDto.setTotal(page.getTotal());

        baseDto.setList(WoniuBeanUtils.copyList(page.getRecords(), dto));
        return baseDto;
    }

}